﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IPopupService PopupService

<MDCard Class=" pa-2" Style="flex-direction: row;justify-content: space-between;">
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Users.InfoCard Model="Model.UserInfor" Href="@("/examine?id="+Model.Id)" Outline Class="@(InReview?"":"w-100")" />
    <div style="display:flex;">
        @if (InReview)
        {
            <CnGalWebSite.Components.Buttons.MasaButton Text="已读" Icon="mdi-read" Rounded OnClick="@(()=>OnReview( EditRecordReviewState.Reviewed))" />
            <CnGalWebSite.Components.Buttons.MasaButton Text="忽略" Icon="mdi-bell-off" Rounded OnClick="@(()=>OnReview( EditRecordReviewState.Ignored))" />
        }
    </div>
</MDCard>


@code {
    [Parameter]
    public ExaminedNormalListModel Model { get; set; } = new ExaminedNormalListModel();
    [Parameter]
    public EventCallback<ExaminedNormalListModel> OnReviewed { get; set; }
    [Parameter]
    public bool InReview { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override void OnParametersSet()
    {
        if (Model != null)
        {
            Model.UserInfor.PersonalSignature = "对『" + (string.IsNullOrWhiteSpace(Model.RelatedName) ? ("Id：" + Model.RelatedId) : Model.RelatedName) + "』 " + Model.Operation.GetDisplayName();
        }
    }

    public async Task OnReview(EditRecordReviewState state)
    {
        //上传
        try
        {
            var obj = await _httpService.PostAsync<EditUserReviewEditRecordStateModel, Result>("api/examines/EditUserReviewEditRecordState", new EditUserReviewEditRecordStateModel
                {
                    State = state,
                    ExamineIds = new long[] { Model.Id }
                });

            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastAsync("审阅失败，" + obj.Error, AlertTypes.Error);
            }
            else
            {
                await OnReviewed.InvokeAsync(Model);
            }
        }
        catch (Exception ex)
        {

            await ErrorHandler.ProcessError(ex, "审阅失败");
        }
    }

}
